Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start writing config pluck helpers #2514

Merged
merged 12 commits into from
May 10, 2024
Merged

Start writing config pluck helpers #2514

merged 12 commits into from
May 10, 2024

Conversation

hadley
Copy link
Member

@hadley hadley commented May 6, 2024

The next part of #1927.

This is not yet complete, but I think it's a good place for another review because I'm starting to set up the API for typed extraction of components from _pkgdown.yml.

@olivroy I'd love your thoughts too 😄

@hadley hadley requested a review from maelle May 6, 2024 21:38
Copy link

github-actions bot commented May 6, 2024

@@ -54,7 +54,7 @@ init_site <- function(pkg = ".") {

copy_assets <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)
template <- purrr::pluck(pkg$meta, "template", .default = list())
template <- config_pluck(pkg, "template")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default to NULL for consistency; there shouldn't be any functional difference because NULL$foo and list()$foo both return NULL.

@@ -110,7 +110,7 @@ test_that("data_navbar() can remove elements", {
right: ~
")

expect_snapshot(data_navbar(pkg))
expect_equal(data_navbar(pkg)$right, "")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with an equality test since we know what we expect here, and it's simple.

package <- purrr::pluck(pkg, "meta", "template", "package")
if (!is.null(package)) {
package <- config_pluck_string(pkg, "template.package")
if (package != "") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (package != "") {
if (nzchar(package)) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I find that style hard to read

pkg,
"Can't find {cli::qty(missing)} component{?s} {.field {msg_flds}}.",
call = call
config_pluck_character <- function(pkg,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be called config_pluck_chr()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the string one, config_pluck_str()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are more matches to rlang's check_ helpers than purrr's, because they're about selecting single values (that are sometimes vectors).

error_call = caller_env()) {
if (is.character(x)) {
x
} else if (identical(x, list())) {
character()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know 😄 But I think it must be some yaml special case where we want to handle an empty list.

tests/testthat/test-config.R Outdated Show resolved Hide resolved
R/config.R Outdated
error_path,
error_call = caller_env()) {
if (is_list(x)) {
if (!is.null(names) && !all(has_name(x, names))) {
Copy link
Collaborator

@maelle maelle May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on my confusion in the tests before I read the error snapshots:

Suggested change
if (!is.null(names) && !all(has_name(x, names))) {
some_required_components_absent <- (!is.null(names) && !all(has_name(x, names)))
if (some_required_components_absent) {

names is maybe not specific enough, could it be "required" or so.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe has_names?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has_required? names is a bit vague

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried has_names and I think it's pretty clear when read in context.

@hadley hadley merged commit e933529 into main May 10, 2024
15 checks passed
@hadley hadley deleted the config-pluck-helpers branch May 10, 2024 15:26
SebKrantz pushed a commit to SebKrantz/pkgdown that referenced this pull request Jun 1, 2024
* Refactor `yaml_character()` to `config_pluck_character()`
* Implement new `config_check_list()`
* Implement `config_pluck_string()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants